package com.lvcoding.cloud.common.security.util;

import com.lvcoding.cloud.common.security.auth.CommonUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 安全工具类
 */
@UtilityClass
public class SecurityUtil {

    /**
     * 获取当前登录用户信息
     * @return
     */
    public CommonUser getCurrentLoginUser(){
        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof CommonUser){
            return (CommonUser) principal;
        }
        return null;
    }


    /**
     * 获取认证信息
     * @return
     */
    public Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }


}
